import Image, ImageFilter, ImageOps, ImageEnhance
from scale_dialog import ScaleDialog
from preview_worker import PreviewWorker

class EffectsMenu:
    def __init__(self, editor, widgets):
        # Store local vars.
        self.editor = editor

        # Fetch button widgets.
        sharpen_button = widgets.get_widget('Editor:SharpenButton')
        blur_button = widgets.get_widget('Editor:BlurButton')
        color_button = widgets.get_widget('Editor:ColorButton')
        brightness_button = widgets.get_widget('Editor:BrightnessButton')
        contrast_button = widgets.get_widget('Editor:ContrastButton')
        invert_button = widgets.get_widget('Editor:InvertButton')
        cancel_button = widgets.get_widget('Editor:EffectsCancelButton')
        
        # Connect signals and slots.
        sharpen_button.connect('clicked', self.sharpen_button_clicked)
        blur_button.connect('clicked', self.blur_button_clicked)
        color_button.connect('clicked', self.color_button_clicked)
        brightness_button.connect('clicked', self.brightness_button_clicked)
        contrast_button.connect('clicked', self.contrast_button_clicked)
        invert_button.connect('clicked', self.invert_button_clicked)
        cancel_button.connect('clicked', self.cancel_button_clicked)
        
    def do_preview_op(self, op, args):
        # Switch to the spinner box.
        self.editor.effects_vbox.hide()
        self.editor.spinner_vbox.show()
        # Spawn a worker to do the heavy lifting.
        PreviewWorker(op, args, self.editor).start()
        # Add the operation to the list.
        self.editor.add_operation((op, args))
        
    def sharpen_button_clicked(self, widget):
        self.do_preview_op('sharpen', None)
    
    def blur_button_clicked(self, widget):
        self.do_preview_op('blur', None)        
    
    def color_button_clicked(self, widget):
        # Pop up the scale dialog.
        sd = ScaleDialog("Color...", 0, 200, 100)
        factor = sd.do_it()
        # Apply the effect.
        if factor != -1:
            self.do_preview_op('color', factor/100)
        else:
            # Quit back to the main menu.
            self.editor.effects_vbox.hide()
            self.editor.main_vbox.show()
                
    def brightness_button_clicked(self, widget):
        # Pop up the scale dialog.
        sd = ScaleDialog("Brightness...", 0, 200, 100)
        factor = sd.do_it()
        # Apply the effect.
        if factor != -1:
            self.do_preview_op('brightness', factor/100)
        else:
            self.editor.effects_vbox.hide()
            self.editor.main_vbox.show()
        
    def contrast_button_clicked(self, widget):
        # Pop up the scale dialog.
        sd = ScaleDialog("Contrast...", 0, 200, 100)
        factor = sd.do_it()
        # Apply the effect.
        if factor != -1:
            self.do_preview_op('contrast', factor/100)
        else:
            self.editor.effects_vbox.hide()
            self.editor.main_vbox.show()

    
    def invert_button_clicked(self, widget):
        self.do_preview_op('invert', None)        
    
    def cancel_button_clicked(self, widget):
        self.editor.effects_vbox.hide()
        self.editor.main_vbox.show()
    